package com.project.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.mapper.Mapper;
import com.project.po.PostrecordInfo;
import com.project.po.postrecord;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Repository
public class PRdao {

    @Resource
    private Mapper mapper;

    /**
     * 通过eid来查询
     * @param eid
     * @return
     */
   public List<PostrecordInfo> getPostRecord(String eid) {
       List<PostrecordInfo> maps = mapper.GetById(eid);
        return maps;
   }

    /**
     * 查询所有的postrecord记录
     * @return
     */
    public List<PostrecordInfo> getAll(){
        List<PostrecordInfo> maps = mapper.Getall();
        return maps;
    }

    /**
     * 添加postrecord记录
     * @param postrecord1
     * @return
     */
    public List<postrecord> add(postrecord postrecord1){
        mapper.insert(postrecord1);
        List<postrecord> maps =mapper.selectList(null);
        return maps;
    }

    /**
     * 来下载到Excel文件中
     *
     * @param fileName
     * @param postrecordList
     * @throws Exception
     */
    public void exportExel(String fileName, List<PostrecordInfo>postrecordList) throws Exception{
        //没有元素，则不进行下载
        if (postrecordList.size()<=0)
            return;
        //新建一个工作簿
        Workbook workbook = new HSSFWorkbook();
        //创建sheet;
        Sheet sheet = workbook.createSheet();
        Row rowT = sheet.createRow(0);
        Cell cellT1 = rowT.createCell(0);
        cellT1.setCellValue("eid");
        Cell cellT2 = rowT.createCell(1);
        cellT2.setCellValue("员工姓名");
        Cell cellT3 = rowT.createCell(2);
        cellT3.setCellValue("pchangedate");
        Cell cellT4 = rowT.createCell(3);
        cellT4.setCellValue("pchangebefore");
        Cell cellT5 = rowT.createCell(4);
        cellT5.setCellValue("pchangedafter");


        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        String sDate;
        for (int i = 0;i<postrecordList.size();i++){
            Row row = sheet.createRow(i+1);
            Cell cell1 = row.createCell(0);
            cell1.setCellValue(postrecordList.get(i).getEid());
            Cell cell2 = row.createCell(1);
            cell2.setCellValue(postrecordList.get(i).getEname());
           Cell cell3 = row.createCell(2);
           sDate=ft.format(postrecordList.get(i).getPchangedate());
            cell3.setCellValue(sDate);
            Cell cell4 = row.createCell(3);
            cell4.setCellValue(postrecordList.get(i).getPchangebefore());
            Cell cell5 = row.createCell(4);
            cell5.setCellValue(postrecordList.get(i).getPchangeafter());


        }
        fileName = fileName +".xls";
        FileOutputStream fout = new FileOutputStream(fileName);
        workbook.write(fout);
        fout.close();
        workbook.close();
    }

}
